﻿using Microsoft.SharePoint;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Grauers_set_metadata_properties.Grauers
{
    public class DocumentPropertyBusinessLayer
    {
        static string metadataPropertiesSettings = "metadataPropertiesSettings";
        public static void Save(DocumentPropertyItem dpi)
        {
            string newStringSaveSettings = dpi.Library +"|" +dpi.Issue +"|" +dpi.ApprovedBy +"|" +dpi.ApprovedDate;
            string metadataProperties = SPContext.Current.Web.Properties[metadataPropertiesSettings] != null ? SPContext.Current.Web.Properties[metadataPropertiesSettings] : "";
                        
            string valueToSet = string.Empty;
            if (metadataProperties.Length > 0)
                valueToSet = metadataProperties + ";" + newStringSaveSettings;
            else
                valueToSet = newStringSaveSettings;

            // Sätt webb property!
            SPWeb web = SPContext.Current.Web;
            web.AllowUnsafeUpdates = true;
            if (web.Properties[metadataPropertiesSettings] == null)
                web.Properties.Add(metadataPropertiesSettings, valueToSet);
            else
                web.Properties[metadataPropertiesSettings] = valueToSet;
            web.Properties.Update();
            web.AllowUnsafeUpdates = false;
        }
        public static List<DocumentPropertyItem> GetAll()
        {
            List<DocumentPropertyItem> dpiList = new List<DocumentPropertyItem>();
            string propertyText = SPContext.Current.Web.Properties[metadataPropertiesSettings];

            if (propertyText != null && propertyText != "")
            {
                // Härlig stränghantering! Lycka till och följa koden! /OG
                string[] splitDPI = propertyText.Split(';');
                foreach (string dpiStrings in splitDPI)
                {
                    string[] dpiString = dpiStrings.Split('|');
                    DocumentPropertyItem dpi = new DocumentPropertyItem();
                    // Library
                    if (dpiString.Length > 0)
                        dpi.Library = dpiString[0];
                    // Issue
                    if (dpiString.Length > 1)
                    {
                        dpi.Issue = dpiString[1];
                    }
                    // Approved by
                    if (dpiString.Length > 2)
                    {
                        dpi.ApprovedBy = dpiString[2];
                    }
                    // Approved date
                    if (dpiString.Length > 3)
                    {
                        dpi.ApprovedDate = dpiString[3];
                    }
                    dpiList.Add(dpi);   
                }
            }
            return dpiList;
        }
        public static DocumentPropertyItem GetBy_LibraryName(string name, SPWeb web)
        {
            DocumentPropertyItem dpi = new DocumentPropertyItem();
            string propertyText = web.Properties[metadataPropertiesSettings];

            if (propertyText != null && propertyText != "")
            {
                // Härlig stränghantering! Lycka till och följa koden! /OG
                string[] splitDPI = propertyText.Split(';');
                foreach (string dpiStrings in splitDPI)
                {
                    string[] dpiString = dpiStrings.Split('|');                    
                    // Library
                    if (dpiString.Length > 0)
                    {
                        dpi.Library = dpiString[0];

                        if (dpi.Library == name)
                        {
                            if (dpiString.Length > 1)
                            {
                                dpi.Issue = dpiString[1];
                            }
                            // Approved by
                            if (dpiString.Length > 2)
                            {
                                dpi.ApprovedBy = dpiString[2];
                            }
                            // Approved date
                            if (dpiString.Length > 3)
                            {
                                dpi.ApprovedDate = dpiString[3];
                            }
                            break;
                        }
                    }                    
                }
            }
            return dpi;
        }
        public static DocumentPropertyItem GetBy_Index(int index, SPWeb web)
        {
            DocumentPropertyItem dpi = new DocumentPropertyItem();
            string propertyText = web.Properties[metadataPropertiesSettings];

            if (propertyText != null && propertyText != "")
            {
                // Härlig stränghantering! Lycka till och följa koden! /OG
                string[] splitDPI = propertyText.Split(';');
                if (index < splitDPI.Length)
                {
                    // Library: 0
                    // Issue: 1
                    // Approved by: 2
                    // Approved date: 3
                    string[] dpiString = splitDPI[index].Split('|');
                    
                    if (dpiString.Length >= 1)
                    {
                        dpi.Library = dpiString[0];
                    }

                    if (dpiString.Length > 1)
                    {
                        dpi.Issue = dpiString[1];
                    }
                    // Approved by
                    if (dpiString.Length > 2)
                    {
                        dpi.ApprovedBy = dpiString[2];
                    }
                    // Approved date
                    if (dpiString.Length > 3)
                    {
                        dpi.ApprovedDate = dpiString[3];
                    }
                }
            }
            return dpi;
        }
        public static void Update(List<DocumentPropertyItem> dpiList)
        {
            int i = 1;
            StringBuilder dpiString = new StringBuilder();
            foreach (DocumentPropertyItem dpi in dpiList)
            {
                dpiString.Append(dpi.Library+ "|" + dpi.Issue+ "|" + dpi.ApprovedBy +"|" +dpi.ApprovedDate);
                if (i++ != dpiList.Count)
                    dpiString.Append(";");
            }
            // Sätt webb property!
            SPWeb web = SPContext.Current.Web;
            web.AllowUnsafeUpdates = true;
            if (web.Properties[metadataPropertiesSettings] == null)
                web.Properties.Add(metadataPropertiesSettings, dpiString.ToString());
            else
                web.Properties[metadataPropertiesSettings] = dpiString.ToString();
            web.Properties.Update();
            web.AllowUnsafeUpdates = false;
        }
    }
}
